import axios from '@ohos/axios';
import { AnyObject } from '../../types';

const request = axios.create({
  baseURL: 'https://music-api.gdstudio.xyz',
  timeout: 10000
})

request.interceptors.request.use((config) => {
  return config
})

request.interceptors.response.use((response) => {
    if (response.data) {
      return response.data;
    } else {
      console.log('response: ' + JSON.stringify(response));
      return Promise.reject(response.data.message);
    }
  },
  (error) => {
    console.log('error: ' + error.message)
    return Promise.reject(error.message);
  })

export default class Http {
  get<T>(url: string, params?: AnyObject): Promise<T> {
    return request.get<any, T>(url, { params })
  }

  post<T>(url: string, data: AnyObject = {}): Promise<T> {
    return request.post<any, T>(url, data)
  }
}